(ns main.admin-view
  (:use 
    main.views
    main.db
    main.manager-view
    ring.util.response
    [hiccup page-helpers form-helpers]
    sandbar.stateful-session))

(defn admin-view 
  [passcode & content]
  (try
  (if-not (.equals ((user-session) :role) "Administrator")
    (redirect "/")
    (layout "Administrator" 
            [:table.left [:tr 
                            [:td [:a {:href "/admin/articles"}"Articles"]]
                            [:td [:a {:href "/admin/categories"}"Article Categories"]]
                            [:td [:a {:href "/admin/users"}"Users"]]]]
             [:table.right [:tr [:td [:a {:href "/lock"}"Lock"]]]]
            [:div#clear]
            content
            [:div#clear]))
  (catch NullPointerException e (show-error "Access denied!"))))

(defn admin-view-articles []
  (admin-view "Articles"
  [:div#articles
           [:fieldset {:style "width: 970px;"} [:legend "Articles"]
            [:div.right (add-article-form)]
            [:div.left (admin-articles {})]
            ]]))

(defn admin-view-categories []
  (admin-view "Categories"
  [:div#articlecategories 
           [:fieldset [:legend "Article Categories"]
            [:div.left 
             (categories-tables "Kuhinja")
             (categories-tables "Sank")
             (admin-articles {:sub-category (session-get :selected-category)})
             ]]]))

(defn admin-view-users []
  (admin-view "Users"
  [:div#users
           [:fieldset [:legend "Users"]
            [:div.left (users-table {})]
            [:div {:style "float: right; border: solid green 1px; border-radius: 5px; padding: 10px;"}
           (form-to [:post "/admin/adduser"]
                    [:p [:label {:for "fname"} "First name"][:br] (text-field {:required "required"} :fname) ]
                    [:p [:label {:for "lname"} "Last name"][:br] (text-field {:required "required"} :lname) ]
                    [:p [:label {:for "passcode"} "Passcode"][:br] (text-field {:required "required" :min "5"} :passcode) ]
                    [:p [:label {:for "role"} "Role"][:br] (drop-down :role ["Waiter" "Manager" "Administrator"]) ]
                    [:p (submit-button {:id "button"} "Add user")]
                    )]
            ]]))



(defn admin-view-add-article [label price instock quantity sub-category category]
  (if (.equals (validate (required label) (validateDouble price) (validateInstock instock) (required quantity)
                (required sub-category) (required category)) "true")
    (do
      (add-article label (Double/parseDouble price) (transform-instock instock) quantity sub-category category)
      (admin-view-articles))
    (show-error (flash-get :error-message))))

(defn admin-view-delete-article [code]
  (do
    (delete-article code)
    (if (.equals ((user-session) :role) "Administrator")
      (redirect "/admin/articles")
    (if (.equals ((user-session) :role) "Manager")
      (redirect "/manager")))))

(defn admin-view-add-user [fname lname passcode role]
  (if (== (user-count passcode) 0)
    (if (.equals (validate (required fname) (required lname) (required passcode) (required role)) "true")
      (do
        (save-user fname lname passcode role)
        (admin-view-users))
      (show-error (flash-get :error-message)))
    (show-error (str "Passcode " passcode " is taken, chosse another"))))

(defn admin-view-remove-user [id]
  (do
    (delete-user id)
    (if (.equals ((user-session) :role) "Administrator")
      (redirect "/admin/users")
    (if (.equals ((user-session) :role) "Manager")
      (redirect "/manager/waiters")))))

(defn admin-view-edit-article [label price instock code quantity sub-category category]
  (if (.equals (validate (required label) (validateDouble price) (validateInstock instock) (required quantity)
                (required sub-category) (required category)) "true")
    (do
      (update-article label (Double/parseDouble price) (transform-instock instock) code quantity sub-category category)
      (article-view code))
    (show-error (flash-get :error-message))))

(defn admin-view-edit-user [id fname lname date-start date-end passcode role]
  (if (.equals (validate (required fname) (required lname) (required passcode)) "true")
    (do
      (update-user id fname lname date-start date-end passcode role)
      (user-view id))
    (show-error (flash-get :error-message))))

(defn set-category-in-session [category]
  (do
    (session-put! :selected-category category)
    (admin-view-categories)))

